﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;
using AimGameEngine.EditorObject;

namespace AimGameEngine.NodeEditorObject
{
    public class NodeTexture2D : NodeBaseControl
    {

        public NodeTexture2D(SpriteBatch inputspritebatch, Camera inputcamera, NodeData inputData
            , List<IDraw> MyListDraw, List<IUpdateMainTime> MyListUpdateTime, Dictionary<String, NodeObject> MyListObject, Dictionary<String, NodeObject> MyListType)
            : base(inputspritebatch, inputcamera, inputData, MyListDraw,MyListUpdateTime , MyListObject , MyListType)
        {
            Connecter inputTexture = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.Texture2D, this);
            ListConnectpoint.Add(inputTexture);
        }

        public override void ShowProprety(ArrayList Temp, CallBack input)
        {
            base.ShowProprety(Temp, input);
            PropretyTexture2D textureproprety = new PropretyTexture2D(this);
            //textureproprety.Deactivate += new EventHandler(input.Deactivate);
            textureproprety.okButton.Click += new EventHandler(input.Deactivate);
            textureproprety.ShowDialog();
        }

        public override void SaveData(ArrayList Thislist, SaveMode inputsavemode)
        {
            base.SaveData(Thislist,inputsavemode);

            if (MyData.Data != null)
                Thislist.Add(MyData.Data);
            else Thislist.Add("NULL");
        }

        public override void LoadData(Identification getdata, int IndexData)
        {
            base.LoadData(getdata, IndexData);

            if (getdata.Items.GetValue(IndexData + 1).GetType() == typeof(String))
            MyData.Data = (String)getdata.Items.GetValue(IndexData + 1);
        }

        public System.Drawing.Image GetImage()
        {
            if (File.Exists(MyData.Data))
            {
                //FileStream fs = new FileStream(MyData.Data, FileMode.Open);
                //System.Drawing.Image bmp2 = System.Drawing.Bitmap.FromStream(fs);
                System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(MyData.Data);
                return bmp2;
            }
            else return null;
        }

        private System.Drawing.Image Texture2Image(Texture2D texture)
        {
            //if (texture == null)
            //{
            //    return null;
            //}

            //if (texture.IsDisposed)
            //{
            //    return null;
            //}

            ////Memory stream to store the bitmap data.
            //MemoryStream ms = new MemoryStream();

            ////Save the texture to the stream.

            //texture.Save(ms, ImageFileFormat.Png);
            
            //texture.SaveAsPng(ms, texture.Width, texture.Height);

            ////Seek the beginning of the stream.
            //ms.Seek(0, SeekOrigin.Begin);

            ////Create an image from a stream.
            //System.Drawing.Image bmp2 = System.Drawing.Bitmap.FromStream(ms);

            ////Close the stream, we nolonger need it.
            //ms.Close();
            //ms = null;
            //return bmp2;
            return null;
        }

        static Microsoft.Xna.Framework.Color[] GetColorDataFromTexture(Texture2D texture) 
        { 
            Microsoft.Xna.Framework.Color[] colors = new Color[texture.Width * texture.Height]; 
            texture.GetData(colors); 
            return colors; 
        } 

        public Texture2D GetTexture()
        {
            if (File.Exists(MyData.Data))
                return TextureFromFile(MyData.Data);
            else return null;
        }

        Texture2D TextureFromFile(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            Texture2D t2d = Texture2D.FromStream(spritebatch.GraphicsDevice, fs);
            fs.Close();
            return t2d;
        }
    }
}
